package com.evil.kingdom.pojo.enums;

import com.evil.common.core.exception.BusinessException;
import com.evil.kingdom.pojo.entity.config.ConfigValue;
import com.evil.kingdom.pojo.entity.config.value.Almanac;
import com.evil.kingdom.pojo.enums.result.RCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * 系统配置
 *
 * @author liyang
 * @date 2024-06-02 19:00
 */
@Getter
@AllArgsConstructor
public enum ConfigEnum {

    /**
     * 黄历
     */
    ALMANAC(Almanac.class),
    ;

    private final Class<? extends ConfigValue> valueClass;

    public static Optional<ConfigEnum> isExistByName(String name) {
        if (StringUtils.isBlank(name)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.name().equals(name)).findFirst();
    }

    public static ConfigEnum findByName(String name) {
        return ConfigEnum.isExistByName(name).orElseThrow(ConfigEnum::error);
    }

    public static <T extends ConfigValue> Optional<ConfigEnum> isExistByValueClass(Class<T> valueClass) {
        if (null == valueClass) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.valueClass.equals(valueClass)).findFirst();
    }

    public static <T extends ConfigValue> ConfigEnum findByValueClass(Class<T> valueClass) {
        return ConfigEnum.isExistByValueClass(valueClass).orElseThrow(ConfigEnum::error);
    }

    public static BusinessException error() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "socket消息类型错误");
    }

}
